package com.tt.sso.service.impl;

import com.tt.common.Const.TtConstans;
import com.tt.common.exception.TtException;
import com.tt.common.page.TtResult;
import com.tt.common.util.IDUtils;
import com.tt.common.util.JSONUtil;
import com.tt.mapper.UserMapper;
import com.tt.pojo.User;
import com.tt.sso.jedis.JedisClient;
import com.tt.sso.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 用户业务
 * @author 青青
 */
@Service
public class UserServiceImpl implements IUserService
{

    @Autowired
    private JedisClient jedisClient;

    @Autowired
    private UserMapper userMapper;

    @Value("${SESSION_EXPIRE}")
    private Integer SESSION_EXPIRE;


    @Override
    public TtResult checkData(String data, int type)
    {
        //1.判断用户名是否可用

        //2判断手机号是否可以使用

        //3判断邮箱是否可以使用

        //执行查询
        User user = new User();
        user.setUsername(data);
        List<User> users = userMapper.selectByUser(user);
        if (!StringUtils.isEmpty(users))
        {
            return TtResult.ok();
        }

        //数据可以使用 返回OK
        return TtResult.ok(false);
    }

    @Override
    public TtResult register(User user)
    {
        user.setCreated(new Date());
        String md5Pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Pass);
        int i = userMapper.insertUser(user);
        return TtResult.ok(i);
    }

    @Override
    public TtResult login(String username, String password) throws TtException
    {
        try
        {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            List<User> users = userMapper.selectByUser(user);
            if (!CollectionUtils.isEmpty(users))
            {
                users.get(0).setPassword(null);

                //生成一个token
                String uuid = IDUtils.getUUID();

                //把用户信息保存到redis，key就是token，value就是用户信息
                String key = TtConstans.gen(uuid);
                jedisClient.set(key, JSONUtil.objectToJson(user));
                jedisClient.expire(key,SESSION_EXPIRE);
                return TtResult.ok(users);
            }
            return TtResult.ok(false);
        }catch (Exception e)
        {
            e.printStackTrace();
            throw new TtException("出现异常："+e.getMessage());
        }

    }

    @Override
    public TtResult getUserByToken(String token)
    {
        String userJSON = TtConstans.genUserToken(token);
        String t = jedisClient.get(userJSON);
        if (StringUtils.isEmpty(t))
        {
            return TtResult.build(400, "用户登录已经过期");
        }
        //重置Session的过期时间
        jedisClient.expire(TtConstans.gen(token), SESSION_EXPIRE);

        //把json转换成User对象
        User user = JSONUtil.jsonToPojo(userJSON, User.class);
        return TtResult.ok(user);
    }
}
